/**
 * @author Hemant
 * Attribute.java
 * This class is for processing tag atributes. 
 * It gets called whenever there is an Attribute type object found.
 * This class has Attribute Object and Hashtable containing objects and its properties.
 * It extracts properties for attribute object and then generates HTML code using those properties. 
 */


/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guiproperty;

import java.util.Hashtable;
import java.util.LinkedList;

import dbmanager.DBManager;
import dbmanager.GetIdName;
import utility.CapitalChar;
import utility.Input;

public class Attribute implements HTMLInterface
{
	private LinkedList<java.lang.Object> PropertyList;
	Hashtable<Object, Object> hash;
	private String header,ObjectName,line = "",text;
	GetIdName get;
	
	/**
	 * This method sets object name for the object name which is present 
	 * in hashtable with its properties. 
	 * @param Objectname Object name of attribute object
	 */
	public void setObjectName(String ObjectName) 
	{
		this.ObjectName = ObjectName;
	}
		
	/**
	 * This method sets GetIdName class object. 
	 * @param get Object of GetIdName class
	 */
	public void setGetIdName(GetIdName get) 
	{
		this.get = get;
	}
	
	/**
	 *
	 * This method sets data line. 
	 * @param line Line od data from file.
	 */
	public void setLine(String line)
	{
		this.line = line;
	}
	
	/**
	 * This method accepts Hashtable containing tag objects and its properties.
	 * It extracts the object specific property list.
	 * @param ObjectPropertyList Hashtable containing all objects and its property list
	 */
	public void setObjPropertyList(Hashtable<java.lang.Object, LinkedList<java.lang.Object>> ObjectPropertyList) 
	{
		this.PropertyList = ObjectPropertyList.get(ObjectName);		
	}
	
	/**
	 * It generates HTML string for specific object using its property list 
	 * @return String of HTML tags for that specific tag object 
	 */
	public String getHTMLCode() 
	{
		try {
			StringBuffer buffer = new StringBuffer();
			StringBuffer href_buffer = new StringBuffer("");
			String href = null;
			boolean isReqParam = false;
			try
			{
				int size = PropertyList.size();			
				buffer.append("");
				boolean isNull = false, isValue = false,isText = false;
				// This for loop iterates over propertylist of the attribute object
				for(int i = 0;i < size;i++ )
				{
					//Extract ith element from propertylist and split it on special char to get an array.
					//The array will contain attribute and its value.
					String [] property = PropertyList.get(i).toString().split(Input.SEPARATOR);
					System.out.println("The property arry is "+PropertyList.get(i));
						
					//if attribute value is other than "attribute" then it 
					//appends attribute and its value to response string buffer
					if(!property[1].equalsIgnoreCase("attribute"))
					{
						//if attribute is "?". The "?" is used for defining query string inside attribte.
						// If attribute is "?" then its value and the attribute will get appended into
						// response string buffer
						if(property[0].equalsIgnoreCase("?") )
						{
							isReqParam = true;
							String field = null;
							String param  = null;
							int start_loc = property[1].indexOf("$");
							int end_loc = property[1].lastIndexOf("$");
							
							if(property[1].startsWith("$") && property[1].endsWith("$") && (start_loc != end_loc))
							{
								param = property[1].substring(1, property[1].length()-1);
							}
							
							field = Field.original_text;
							
							try {
								if(field == null || field.equalsIgnoreCase("null"))
								{								
									field = (String)hash.get(param.toLowerCase());
								}						
							
								if(field != null || !field.equalsIgnoreCase("null") )
								{
									if(param != null)
										href_buffer.append(param+"="+field+"&");
									else
										href_buffer.append(property[1]+"="+field+"&");
									System.out.println("href buffer is  "+href_buffer);
								}
							} catch (NullPointerException e) {
								isReqParam = false;
							}
						}
						else if(property[0].equalsIgnoreCase("href"))
						{
							href = property[1];
						}
						else if(property[0].equalsIgnoreCase("src"))
						{
							String temp = null;
							try
							{
								temp = property[1];//Input.Web_Reports+property[1]+".html";
							}
							catch(Exception e)
							{
								temp = property[1];
							}
							
							buffer.append(property[0]+"=\""+temp+"\" ");	
						}			
						else if(property[0].equalsIgnoreCase("value"))
						{
							isValue = true;						
							String temp = property[1];
							int start_loc = property[1].indexOf("$");
							int end_loc = property[1].lastIndexOf("$");
							
							if(temp.startsWith("$") && temp.endsWith("$") && (start_loc != end_loc))
							{
								String param = temp.substring(1, temp.length()-1);
								try {
									buffer.append(property[0]+" =\""+(String)hash.get(param)+"\"");
								} catch (NullPointerException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							/*else if(temp.equalsIgnoreCase("null") || property[1].equalsIgnoreCase(""))
							{
								isNull = true;
							}*/
							else
							{
								buffer.append(property[0]+" =\""+property[1]+"\" ");
							}
						}	
						/*else if(property[0].equalsIgnoreCase("text"))
						{
							isText = true;
							this.text = property[1];						
						}*/
						else
						{
							buffer.append(property[0]+" =\""+property[1]+"\" ");
						}
					}				
				}
				
				if(href != null)
					buffer.append("href=\""+href+"\"");
				if(isReqParam)
					buffer = new StringBuffer(buffer.substring(0, buffer.length()-1)+"?"+href_buffer.subSequence(0, href_buffer.length()-1)+"\"");
				
				/*if(isNull && isValue && isText)
				{
					System.out.println(hash);
					System.out.println("Text is "+text+"//");
					String temp = (String)hash.get(text.toLowerCase());
					if(temp != null)
						buffer.append("value =\""+CapitalChar.makeFirstCharCapital(temp)+"\" ");
				}*/
				
				System.out.println("The buffer in Attribute class is "+buffer.toString());
			}
			catch (NullPointerException e)
			{
				e.printStackTrace();
				buffer = new StringBuffer("");
			}
			return buffer.toString();
		} catch (Exception e) {
			return "";
		}
	}
	
	
	public void setDisplayText(String displaytext) {
		
	}
		
	public void setTag(String tag) {
		
	}
	
	public void setReport(String report) {
		
	}
	public void setParam(String param) {
	        
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setParamHash(Hashtable<Object, Object> hash) {
		// TODO Auto-generated method stub
		this.hash = hash;
	}

	public void setNoSelectObject(String noselect) {
		// TODO Auto-generated method stub		
	}

	public void setSelectObject(String select) {
		// TODO Auto-generated method stub		
	}

	public void isEnd(boolean end) {
		// TODO Auto-generated method stub		
	}

	public void setHeaderLine(String header) {
		// TODO Auto-generated method stub
		this.header = header;
	}
	
}
